﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TileEngine.tiles.material;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using TileEngine.tiles;

namespace TileEngine
{
    class MapRow
    {
        public List<MapCell> Columns = new List<MapCell>();
        
    }

    class Map
    {
        public List<MapRow> Rows = new List<MapRow>();
        public int MapWidth = 400;
        public int MapHeight = 300;
        public MapCell this[int x,int y]
        {
            get {
                return Rows[(y + 2*(int)MapHeight) % (int)MapHeight].Columns[(x + 2*(int)MapWidth) % (int)MapWidth]; 
            }
            set { Rows[y].Columns[x] = value; }
        }


        public Map(int MapWidth,int MapHeight)
        {
            this.MapHeight = MapHeight;
            this.MapWidth = MapWidth;
            for (int y = 0; y < MapHeight; y++)
            {
                MapRow thisRow = new MapRow();
                for (int x = 0; x < MapWidth; x++)
                {
                    thisRow.Columns.Add(new MapCell(new Dirt(x,y),new DirtWall(x,y)));
                }
                Rows.Add(thisRow);
            }
        }
        public void draw(SpriteBatch spriteBatch,int firstX,int firstY,int offsetX,int offsetY)
        {
            
            //LightEngine.calculateLights(new Point(firstX-10,firstY-10),new Point(firstX+10+(int)Camera.tilesOnScreen.X,firstY+10+(int)Camera.tilesOnScreen.Y));
            for (int y = 0; y < Camera.tilesOnScreen.Y + 1; y++)
            {
                for (int x = 0; x < Camera.tilesOnScreen.X + 1; x++)
                {
                    if (this[x + firstX, y + firstY].wall != null &&  (this[x + firstX, y + firstY].tile.GetType()==typeof(Empty) || this[x + firstX, y + firstY].tile.isWallvisible()))
                        this[x + firstX, y + firstY].wall.draw(spriteBatch, new Point((x * Global.tileSize.X) - offsetX, (y * Global.tileSize.Y) - offsetY), LightEngine.getColor(x, y));
                    if (this[x+firstX,y+firstY].tile != null)
                        this[x + firstX, y + firstY].tile.draw(spriteBatch, new Point((x * Global.tileSize.X) - offsetX, (y * Global.tileSize.Y) - offsetY), LightEngine.getColor(x, y));
                    if (this[x + firstX, y + firstY].liquid != null)
                        this[x + firstX,y + firstY].liquid.draw(spriteBatch, new Rectangle((x * Global.tileSize.X) - offsetX, (y * Global.tileSize.Y) - offsetY, Global.tileSize.X, Global.tileSize.Y),LightEngine.getColor(x,y));
                   
                }
            }
        }
    }
}
